package cn.ekuma.epos.qkorder.link;

import javax.swing.JPanel;
import com.openbravo.bean.erp.Order;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.pos.base.AppView;
import com.openbravo.pos.panels.event.ProductChangedListener;
import com.openbravo.pos.panels.link.I_DeleteAblePanel;

import cn.ekuma.epos.datalogic.I_DataLogicSystem;
import cn.ekuma.epos.order.JOrderLines;
import cn.ekuma.epos.qkorder.JLinkContainerPanel;
import cn.ekuma.epos.qkorder.JPanelOrder;

import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class JOrderRefPanel extends JPanel implements I_DeleteAblePanel{

	private AppView m_App;
	private JOrderLines m_Orderlines;
	Order m_oOrder;
	I_DataLogicSystem dlSystem;
	boolean editModel;
    private  JPanelOrder jPanelOrder;
	/** Creates new form JOrderRefPanel */
    public JOrderRefPanel(JLinkContainerPanel container,Order m_oOrder,boolean editModel,I_DataLogicSystem dlSystem) {
        initComponents();
        this.jPanelOrder=container.getjPanelOrder();
        this.m_oOrder=m_oOrder;
        this.editModel=editModel;
        m_App=container.getApp();
        this.dlSystem = dlSystem;
        buildOrderLinePanel();
        loadLines();
    }
    
    
    public void loadLines(){
    	m_Orderlines.clearOrderLines();
        for (int i = 0; i < m_oOrder.getLinesCount(); i++) {
            m_Orderlines.addOrderLine(m_oOrder.getLine(i));
        } 
    }
    
    private void buildOrderLinePanel(){
        String lineStyle="Order.Line";
        String lineColumnXML=dlSystem.getResourceAsXML(lineStyle+"."+m_oOrder.getDefineType().getName()+"."+m_App.getAppUserView().getUser().getName());
        if(lineColumnXML==null||lineColumnXML.isEmpty())
            lineColumnXML=dlSystem.getResourceAsXML(lineStyle+"."+m_oOrder.getDefineType().getName());
        if(lineColumnXML==null||lineColumnXML.isEmpty())
            lineColumnXML=dlSystem.getResourceAsXML(lineStyle); 
        m_Orderlines.init(lineColumnXML);
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        m_jPanelCentral = new javax.swing.JPanel();
        setLayout(new java.awt.BorderLayout());
        m_jPanelCentral.setLayout(new java.awt.BorderLayout());
        
        m_Orderlines = new JOrderLines();
        m_jPanelCentral.add(m_Orderlines, java.awt.BorderLayout.CENTER);
        
        panel = new JPanel();
        m_jPanelCentral.add(panel, BorderLayout.NORTH);
        panel.setLayout(new BorderLayout(0, 0));
        jButton1 = new javax.swing.JButton();
        panel.add(jButton1, BorderLayout.EAST);
        jButton1.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		changeOrder();
        	}
        });
        
       jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/2uparrow.png")));
                
       label = new JLabel("New label");
                panel.add(label, BorderLayout.CENTER);
        /**m_Orderlines.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
               refreshOrderLineSelectedState();
            }
        });*/
                
       add(m_jPanelCentral, java.awt.BorderLayout.PAGE_START);          
    }// </editor-fold>


    protected void changeOrder() {
		Order temp=jPanelOrder.getActiveOrder();
		boolean editAble=jPanelOrder.isOrderEditable();
		try {
			jPanelOrder.getM_Ordersbag().setActiveOrder(m_oOrder, editModel);
			this.editModel=editAble;
			this.m_oOrder=temp;
			buildOrderLinePanel();
			loadLines();
		} catch (BasicException e) {
			new MessageInf(e).show(this);
		}
	}


	// Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel m_jPanelCentral;
    private JLabel label;
    private JPanel panel;
    // End of variables declaration
    
    public void addProductChangedListener(ProductChangedListener l) {
    	m_Orderlines.addProductChangedListener(l);
	}

	public void removeProductChangedListener(ProductChangedListener l) {
		m_Orderlines.removeProductChangedListener(l);
	}


	@Override
	public boolean deactivate() {
		return true;
	}

}
